<template>
  <div class="row g-2">
    <template v-if="Work.length!==0">
      <template v-for="el in Work">
        <div :key="el._id" class="card col-md-4 col-sm-12 overflow-hidden">
          <div class="card-body">
            <p class="card-title"><span class="myName" @click="contestDetail(el.contestID)">{{ el.contest[0].name }}</span></p>
            <h6 class="card-subtitle mb-2 text-muted">作品类型：{{ el.isTeam ? '团队作品' : '个人作品' }}</h6>
            <h6 class="card-subtitle mb-2 text-muted">上传时间：{{ el.updateTime|time }}</h6>
            <h6 class="card-subtitle">作品成绩：<span
                style="color: red;font-weight: 600">{{ el.score.length === 0 ? '未评审' : el.score }}</span>&emsp;<span
                @click="downloadFile(el.path)"
                style="color: #40aaff;text-decoration: underline;cursor: pointer">下载作品</span></h6>
          </div>
        </div>
      </template>

    </template>
    <template v-else>
      <div class="col">
        <el-empty class="border" description="暂无队伍信息"></el-empty>
      </div>
    </template>
  </div>
</template>

<script>
import user from "../../../api/user";
import time from "../../../filter/time";

export default {
  name: "myWork",
  mounted() {
    this.GetMyWork()
  },
  filters: {
    time
  },
  data() {
    return {
      myTeam: JSON.parse(localStorage.getItem("myTeam")),
      Work: [],
    }
  },
  methods: {

    contestDetail(id){
      this.$router.push("/detail?id="+id)
    },

    // 下载个人作品
    downloadFile(obj) {
      const param = {
        name: obj,
        path: obj
      }
      const a = document.createElement("a");
      a.setAttribute("href", "http://127.0.0.1/Admin/DownloadFile?fileInfo=" + JSON.stringify(param));
      let clickEvent = document.createEvent("MouseEvents");
      clickEvent.initEvent("click", true, true);
      a.dispatchEvent(clickEvent);
    },
    GetMyWork() {
      user.GetMyWork().then(r => {
        const data = r.data.data.w
        let myData = []
        data.forEach(v => {
          if (v.userID === this.$store.getters["userStore/userInfo"]._id || v.teamID === this.myTeam._id) {
            myData.push(v)
          }
        })
        this.Work = myData
      })
    }
  }
}
</script>

<style scoped>
.myName:hover {
  cursor: pointer;
  text-decoration: underline;
  color: #40aaff;
}
</style>